﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Evaluate.BLL;
using System.Text;
using System.Data;
using Yipusoft.Evaluate.Model;

public partial class Evaluate_CheckInputD : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "320402"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
        }
        if (!IsPostBack)
        {
            BindYearList();
            CreateTable();
        }
    }
    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedValue = DateTime.Now.Year.ToString();
    }

    private string GetDepartmentID()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;


        return " a.DepartmentID like ''" + DepartmentID.Substring(0, 6) + "%'' And ";

    }
    private void CreateTable()
    {
        string departmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        DataSet ds = SplitPage.ExecuteSQL("SELECT shortName FROM Account_Departments WHERE  DepartmentId like '" + departmentId.Substring(0, 4) + rbType.SelectedValue + "%' order By departmentId");
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN  b.DepartmentId2 = ''' + DepartmentId + ''' THEN b.ItemScore END) AS D' + DepartmentId + '' FROM Account_Departments WHERE DepartmentId like '" + departmentId.Substring(0, 4) + rbType.SelectedValue + "%' order By DepartmentID");
        sql.AppendLine("SET @SQL = 'SELECT a.CheckID, a.CheckName' + @SQL + ' FROM Checks a LEFT JOIN CheckItems b ON a.CheckID = b.CheckID WHERE " + GetDepartmentID() + " a.Flag=" + rbType.SelectedValue + " and YEAR(a.CheckDate)=" + YearList.SelectedValue + " GROUP BY a.CheckID, a.CheckName'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;

        Table tb = new Table();
        tb.Caption = "<span>" + YearList.SelectedItem.Text + "考核验收成绩录入</span>";

        tb.CellPadding = 4;
        tb.CellSpacing = 0;
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        tr.CssClass = "grid_header";
        tc.Text = "";

        tr.Cells.Add(tc);
        tc = new TableCell();
        tc.HorizontalAlign = HorizontalAlign.Left;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Text = "考核验收名称";
        tc.Style.Add("white-space", "nowrap");
        tr.Cells.Add(tc);

        int col = ds.Tables[0].Rows.Count;

        for (int i = 0; i < col; i++)
        {
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;

            tc.Style.Add("writing-mode", "tb-lr");
            tc.Style.Add("white-space", "nowrap");
            tc.Text = ds.Tables[0].Rows[i][0].ToString();
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);
        }
        int row = 1;
        foreach (DataRowView drv in dv)
        {
            CheckInfo info = Check.GetCheck(Convert.ToInt32(drv[0]));
            decimal TotalScore = info.TotalScore;
            tr = new TableRow();
            tr.CssClass = "grid_item";
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.Text = row.ToString();
            tc.Style.Add("white-space", "nowrap");
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.BackColor = System.Drawing.Color.White;
            tc.Text = Check.GetCheck((int)drv[0]).CheckName;
            tr.Cells.Add(tc);
            for (int i = 2; i <= col + 1; i++)
            {

                tc = new TableCell();
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Center;
                string score = "";
                if (drv[i] != DBNull.Value)
                    score = (TotalScore - (decimal)drv[i]).ToString("0");
                else
                    score = TotalScore.ToString("0");
                tc.Text = "<a href=CheckItemInputD.aspx?CheckId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + score + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
            row++;
        }
        PlaceHolder.Controls.Add(tb);
    }


    protected void YearList_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTable();
    }
    protected void rbType_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTable();
    }
}
